package com.blog.controller.manage;

import com.blog.bean.Article;
import com.blog.bean.ArticleCategory;
import com.blog.constant.RoleConstant;
import com.blog.service.ArticleCategoryService;
import com.blog.service.ArticleService;
import com.blog.vo.ArticleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/articleManage")
public class ArticleManageController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleCategoryService articleCategoryService;

    /**
     * 更新文章
     *
     * @param article
     * @return
     */
    @RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    @ResponseBody
    public Object updateArticle(Article article) {
        Map<Object, Object> map = new HashMap<>();
        int count = articleService.updateByPrimaryKeySelective(article);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 根据次分类id，得到主分类
     *
     * @param acSecondId
     * @return
     */
    @RequestMapping("/getArticleCategoryByFirstId")
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    @ResponseBody
    public Object getArticleCategoryByFirstId(@RequestParam(value = "acSecondId") Long acSecondId) {
        Map<Object, Object> map = new HashMap<>();
        ArticleCategory ac = new ArticleCategory();
        ac.setArticleCategoryId(acSecondId);
        ArticleCategory category = articleCategoryService.getArticleCategory(ac);
        ArticleCategory acFather = new ArticleCategory();
        acFather.setArticleCategoryId(category.getArticleCategoryParentId());
        ArticleCategory articleCategory = articleCategoryService.getArticleCategory(acFather);
        if (articleCategory != null) {
            map.put("articleCategory", articleCategory);
        }
        return map;
    }

    /**
     * 更新文章页
     * 加载所有文章主分类
     * 加载当前文章所属次分类
     *
     * @param articleId
     * @param model
     * @return
     */
    @RequestMapping(value = "/updateArticle", method = RequestMethod.GET)
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    public String updateArticle(@RequestParam(value = "articleId") Long articleId, Model model) {
        // 文章
        Article ar = new Article();
        ar.setArticleId(articleId);
        Article article = articleService.getArticle(ar);
        // 文章次分类
        ArticleCategory arca = new ArticleCategory();
        arca.setArticleCategoryId(article.getArticleParentId());
        ArticleCategory arFather = articleCategoryService.getArticleCategory(arca);
        // 文章所有分类
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setArticleCategoryState(0);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(articleCategory);
        List<ArticleCategory> articleCategoryListFirst = new ArrayList<>();
        List<ArticleCategory> articleCategoryListSecond = new ArrayList<>();
        for (ArticleCategory temp : articleCategoryList) {
            // 所有主分类
            if (temp.getArticleCategoryParentId() == 0) {
                articleCategoryListFirst.add(temp);
            }
            // 加载当前文章所属次分类
            if (arFather.getArticleCategoryParentId() == temp.getArticleCategoryParentId()) {
                articleCategoryListSecond.add(temp);
            }
        }
        model.addAttribute("articleCategoryListFirst", articleCategoryListFirst);
        model.addAttribute("articleCategoryListSecond", articleCategoryListSecond);
        model.addAttribute("article", article);
        return "manage-article-update";
    }

    /**
     * 删除文章
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/deleteArticle")
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    @ResponseBody
    public Object deleteArticle(@RequestParam(value = "articleId") Long articleId) {
        Map<Object, Object> map = new HashMap<>();
        int count = articleService.deleteByPrimaryKey(articleId);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 添加文章
     *
     * @param article
     * @return
     */
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    @ResponseBody
    public Object addArticle(Article article) {
        Map<Object, Object> map = new HashMap<>();
        int count = articleService.insertSelective(article);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 点击主分类事件
     * 根据父分类id查询子分类
     *
     * @param articleCategoryParentId
     * @return
     */
    @RequestMapping("/getArticleCategoryByParentId")
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    @ResponseBody
    public Object getArticleCategoryByParentId(@RequestParam(value = "articleCategoryParentId") Long articleCategoryParentId) {
        Map<Object, Object> map = new HashMap<>();
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setArticleCategoryParentId(articleCategoryParentId);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(articleCategory);
        map.put("articleCategoryList", articleCategoryList);
        return map;
    }

    /**
     * 添加文章页
     * 加载主分类
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/addArticle", method = RequestMethod.GET)
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    public String addArticle(Model model) {
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setArticleCategoryParentId((long) 0);
        articleCategory.setArticleCategoryState(0);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(articleCategory);
        model.addAttribute("articleCategoryList", articleCategoryList);
        return "manage-article-add";
    }

    /**
     * 文章列表
     * 封装文章，文章主分类，次分类
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/articleList")
    @RequiresRoles(RoleConstant.ROLE_ADMIN_ARTICLE)
    public String articleList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        List<ArticleVo> articleVoList = new ArrayList<>();
        List<Article> articleList = articleService.selectArticle(new Article());
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(new ArticleCategory());
        for (Article articleTemp : articleList) {
            ArticleVo articleVo = new ArticleVo();
            articleVo.setArticle(articleTemp);
            for (ArticleCategory articleCategoryTemp : articleCategoryList) {
                // 次分类
                if (articleCategoryTemp.getArticleCategoryParentId() != 0) {
                    if (articleTemp.getArticleParentId() == articleCategoryTemp.getArticleCategoryId()) {
                        articleVo.setArticleCategorySecond(articleCategoryTemp);
                        for (ArticleCategory temp : articleCategoryList) {
                            // 主分类
                            if (articleCategoryTemp.getArticleCategoryParentId() == temp.getArticleCategoryId()) {
                                articleVo.setArticleCategoryFirst(temp);
                            }
                        }
                    }
                }
            }
            articleVoList.add(articleVo);
        }
        PageInfo<ArticleVo> pageInfo = new PageInfo<>(articleVoList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-article";
    }
}
